Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a HNSW collector that exits early when nearest neighbor queue saturates #14094

Open
wants to merge 36 commits into
base: main
Choose a base branch
from

Conversation

tteofili
Copy link
Contributor

@tteofili tteofili commented Jan 2, 2025

This introduces a HnswKnnCollector interface, extending KnnCollector for HNSW, to make it possible to hook into HNSW execution for optimizations.
It then adds a new collector which uses a saturation-based threshold to dynamically halt HNSW graph exploration, in order to early exit when the exploration of new candidates is unlikely to lead to addition of new neighbors.
The new collector records the number of added neighbors upon exploration of a new candidate (a HNSW node) and it compares it with the number of neighbors added while exploring the previous candidate, when the rate of added neighbors plateaus for a number of consecutive iterations, it stops graph exploration (earlyTerminate returns true).

@tteofili
Copy link
Contributor Author

Screenshot 2025-01-15 at 14 40 16
this sample graph (from Cohere-768) shows how the collection of nearest neighbors saturates and hence it makes sense to stop visiting the graph "earlier", e.g., when the saturation counter exceeds a given threshold.

Comment on lines 20 to 24
public interface HnswKnnCollector extends KnnCollector {

/** Indicates exploration of the next HNSW candidate graph node. */
void nextCandidate();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this kind of collector is OK. But it makes most sense to me to be a delegate collector. An abstract collector to KnnCollector.Delegate.

Then, I also think that the OrdinalTranslatingKnnCollector should inherit directly from HnswKnnCollector always assuming that the passed in collector is a HnswKnnCollector.

Note, the default behavior for HnswKnnCollector#nextCandidate can simply be nothing, allowing for overriding.

This might require a new HnswGraphSearcher#search interface to keep the old collector actions, but it can be simple to add a new one that accepts a HnswKnnCollector and delegate to it with new HnswKnnCollector(KnnCollector delegate).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I adjusted my refactoring for the seeded queries similarly. It seems nicer IMO: #14170

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks Ben. I'll incorporate your suggestions once #14170 is in.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made HnswKnnCollector a KnnCollector.Decorator in c6dbf7e

@tteofili
Copy link
Contributor Author

tteofili commented Feb 13, 2025

updated results (Cohere-768, 200k docs, merge disabled)

baseline

recall  latency (ms)    nDoc  topK  fanout  maxConn  beamWidth  quantized  visited  index s  index docs/s  num segments  index size (MB)  vec disk (MB)  vec RAM (MB)
 0.946         1.776  200000   100      50       32        100         no    22645    12.45      16070.71            33           593.99        585.938       585.938

candidate

recall  latency (ms)    nDoc  topK  fanout  maxConn  beamWidth  quantized  visited  index s  index docs/s  num segments  index size (MB)  vec disk (MB)  vec RAM (MB)
 0.948         1.424  200000   100      50       32        100         no    19507    12.49      16014.09            33           593.98        585.938       585.938

@tteofili
Copy link
Contributor Author

reference paper

@tteofili tteofili marked this pull request as ready for review February 25, 2025 11:19
@tteofili
Copy link
Contributor Author

I've updated this and moved the early termination logic not to kick in by default but to be based on a (wrapping) PatienceKnnVectorQuery.

@tteofili
Copy link
Contributor Author

tteofili commented Feb 25, 2025

updated lucene_util benchmarks, with different parameters (Cohere-768, ndoc=200k).

maxconn=32

baseline

recall  latency (ms)    nDoc  topK  fanout  maxConn  beamWidth  quantized  visited  index s  index docs/s  num segments  index size (MB)  vec disk (MB)  vec RAM (MB)
 0.963         1.262  200000   100      50       32        100         no     7984    61.61       3246.12             3           595.73        585.938       585.938

candidate@default

recall  latency (ms)    nDoc  topK  fanout  maxConn  beamWidth  quantized  visited  index s  index docs/s  num segments  index size (MB)  vec disk (MB)  vec RAM (MB)
 0.957         1.165  200000   100      50       32        100         no     7171    65.61       3048.36             3           595.73        585.938       585.938
 0.958         1.164  200000   100      50       32        100         no     7214    63.45       3151.99             3           595.68        585.938       585.938

candidate@sat=0.995,patience=maxconn(32)

recall  latency (ms)    nDoc  topK  fanout  maxConn  beamWidth  quantized  visited  index s  index docs/s  num segments  index size (MB)  
 0.952         1.082  200000   100      50       32        100         no     6623    61.71       3240.76             3           595.71        585.938       585.938
 0.952         1.119  200000   100      50       32        100         no     6682    61.04       3276.65             3           595.70        585.938       585.938

candidate@sat=0.95,patience=fanout(50)

recall  latency (ms)    nDoc  topK  fanout  maxConn  beamWidth  quantized  visited  index s  index docs/s  num segments  index size (MB)  vec disk (MB)  vec RAM (MB)
 0.908         0.781  200000   100      50       32        100         no     4499    61.82       3235.30             3           595.72        585.938       585.938
 0.909         0.779  200000   100      50       32        100         no     4498    62.09       3221.34             3           595.69        585.938       585.938

candidate@sat=0.995,patience=fanout(50)

recall  latency (ms)    nDoc  topK  fanout  maxConn  beamWidth  quantized  visited  index s  index docs/s  num segments  index size (MB)  vec disk (MB)  vec RAM (MB)
 0.960         1.178  200000   100      50       32        100         no     7361    63.65       3142.33             3           595.75        585.938       585.938
 0.960         1.195  200000   100      50       32        100         no     7441    62.18       3216.42             3           595.76        585.938       585.938

maxconn=64

baseline

 0.968         1.351  200000   100      50       64        100         no     8698    63.04       3172.79             3           595.76        585.938       585.938
 0.968         1.328  200000   100      50       64        100         no     8744    62.29       3210.94             3           595.77        585.938       585.938

candidate@default

recall  latency (ms)    nDoc  topK  fanout  maxConn  beamWidth  quantized  visited  index s  index docs/s  num segments  index size (MB)  vec disk (MB)  vec RAM (MB)
 0.960         1.193  200000   100      50       64        100         no     7751    62.79       3185.27             3           595.76        585.938       585.938
 0.961         1.213  200000   100      50       64        100         no     7789    61.73       3240.02             3           595.73        585.938       585.938

candidate@sat=0.995,patience=maxconn(64)

 0.965         1.282  200000   100      50       64        100         no     8364    62.86       3181.88             3           595.72        585.938       585.938
 0.964         1.274  200000   100      50       64        100         no     8361    62.42       3203.90             3           595.79        585.938       585.938

candidate@sat=0.95,patience=fanout(50)

recall  latency (ms)    nDoc  topK  fanout  maxConn  beamWidth  quantized  visited  index s  index docs/s  num segments  index size (MB)  vec disk (MB)  vec RAM (MB)
 0.913         0.863  200000   100      50       64        100         no     4945    63.00       3174.80             3           595.81        585.938       585.938
 0.916         0.797  200000   100      50       64        100         no     4965    62.70       3189.89             3           595.78        585.938       585.938

candidate@sat=0.995,patience=fanout(50)

recall  latency (ms)    nDoc  topK  fanout  maxConn  beamWidth  quantized  visited  index s  index docs/s  num segments  index size (MB)  vec disk (MB)  vec RAM (MB)
 0.962         1.226  200000   100      50       64        100         no     7991    64.13       3118.52             3           595.82        585.938       585.938
 0.963         1.236  200000   100      50       64        100         no     7859    62.52       3199.23             3           595.80        585.938       585.938

super(collector);
}

/** Indicates exploration of the next HNSW candidate graph node. */

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: suggest "triggers" instead of "indicates"


@Override
public String toString(String field) {
return null;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps add some name here, maybe in the future it will be easier to debug?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants